package com.neuedu.listener;

import javax.servlet.*;

public class MyServletRequestListener implements ServletRequestListener, ServletRequestAttributeListener {


    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        ServletRequest servletRequest = sre.getServletRequest();
        Object source = sre.getSource();
        System.out.println("MyServletRequestListener.requestInitialized");
        System.out.println("servletRequest = " + servletRequest);
        System.out.println("source = " + source);

    }


    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        ServletRequest servletRequest = sre.getServletRequest();
        Object source = sre.getSource();
        System.out.println("MyServletRequestListener.requestDestroyed");
        System.out.println("servletRequest = " + servletRequest);
        System.out.println("source = " + source);
    }



    //request.setAttribute("key","value1");
    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        String name = srae.getName();
        Object value = srae.getValue();


        System.out.printf("MyServletRequestListener.attributeAdded       name:%s , value:%s\r\n",name,value.toString());
    }


    //request.removeAttribute("key);
    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        String name = srae.getName();
        Object value = srae.getValue();

        System.out.printf("MyServletRequestListener.attributeRemoved       name:%s , value:%s\r\n",name,value.toString());
    }

    //request.setAttribute("key","value2");
    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {
        String name = srae.getName();
        Object value = srae.getValue();  //旧的
        //新的
        Object newValue = srae.getServletRequest().getAttribute(name);

        System.out.printf("MyServletRequestListener.attributeReplaced       name:%s , value:%s    ,newValue :%s  \r\n",name,value.toString() ,newValue.toString());
    }
}
